package com.mobiletrain.org.motherkitch.goumaiqingdan;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.lidroid.xutils.DbUtils;
import com.mobiletrain.org.motherkitch.MyApplication;
import com.mobiletrain.org.motherkitch.R;

import java.util.ArrayList;
import java.util.HashMap;

public class GmqdFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private TextView mTitle;
    private ListView mListView;
    private ArrayList<ShoppingList1> shopList;
    private ArrayList<HashMap<String, String>> listMap;
    private SimpleAdapter simpleAdapter;

    public static GmqdFragment newInstance(String param1, String param2) {
        GmqdFragment fragment = new GmqdFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public GmqdFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_gmqd, container, false);
        mTitle= (TextView) view.findViewById(R.id.list_title);
        mListView= (ListView) view.findViewById(R.id.list_materail);
        MyApplication myApp=new MyApplication();
        DbUtils db = myApp.getDbUtils();
        try {
            shopList= (ArrayList<ShoppingList1>) db.findAll(ShoppingList1.class);

        } catch (Exception e) {
            e.printStackTrace();
        }
        listMap=new ArrayList<HashMap<String,String>>();
        for (int i = 0; i < shopList.size(); i++) {
            HashMap<String, String> map = new HashMap<String,String>();
            mTitle.setText(shopList.get(i).getTitle());
            map.put("name",shopList.get(i).getName());
            map.put("num",shopList.get(i).getNum());

            listMap.add(map);
        }

        simpleAdapter=new SimpleAdapter(getActivity(),listMap,
                R.layout.detail_cook_item,
                new String[]{"name","num"},
                new int[]{R.id.detail_cook_text1,R.id.detail_cook_text2});
        mListView.setAdapter(simpleAdapter);
        return view;
    }


}
